# Build recipe for mksh and lksh.
#
# Copyright (c) 2017-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=mksh
version=R56c
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/shells"

tarname=${program}-${version}.tgz

# Remote source(s)
fetch=http://www.mirbsd.org/MirOS/dist/mir/mksh/$tarname

description="
The MirBSD Korn Shell.

mksh is the MirBSD enhanced version of the Public Domain Korn shell
(pdksh), a Bourne-compatible shell which is largely similar to the
original AT&T Korn shell; mksh is the only pdksh derivate currently
being actively developed.  It includes bug fixes and feature
improvements, in order to produce a modern, robust shell good for
interactive and especially script use.
"

homepage=http://www.mirbsd.org/mksh.htm
license="The MirOS Licence, ISC, special terms for use the BSD daemon mascot"

# Source documentation
docsdir="${docdir}/${program}-${version}"

# Source directory name
srcdir=mksh

build()
{
    set -e

    # Create package structure first
    mkdir -p "${destdir}/etc/skel" \
             "${destdir}/usr/bin"  \
             "${destdir}/${mandir}/man1"

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Create a separate build directory
    rm -rf ../mksh-build ../lksh-build
    mkdir ../mksh-build ../lksh-build

    # Build and install mksh

    cd ../mksh-build
    (
        env CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" LDSTATIC=-static \
        sh ../${srcdir}/Build.sh -r && ./test.sh
    )

    cp -p mksh "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/mksh"

    #cp -p ../${srcdir}/dot.mkshrc "${destdir}/etc/skel/.mkshrc"
    cp -p "${archive}/etc/skel/.mkshrc" "${destdir}/etc/skel/"
    chmod 644 "${destdir}/etc/skel/.mkshrc"

    # To manage (dot new) config file
    touch "${destdir}/etc/skel/.graft-config"

    cp -p ../${srcdir}/mksh.1 "${destdir}/${mandir}/man1/"
    chmod 644 "${destdir}/${mandir}/man1/mksh.1"

    # Build and install lksh (legacy)

    cd ../lksh-build
    (
        env CPPFLAGS="$CPPFLAGS -DMKSH_BINSHPOSIX" \
            CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" LDSTATIC=-static \
        sh ../${srcdir}/Build.sh -L -r && ./test.sh
    )

    cp -p lksh "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/lksh"

    cp -p ../${srcdir}/lksh.1 "${destdir}/${mandir}/man1/"
    chmod 644 "${destdir}/${mandir}/man1/mksh.1"

    # Make symlink to compose the default shell
    ( cd "${destdir}/usr/bin" && ln -sf lksh sh )

    # Compress manual pages
    if test -d "${destdir}/$mandir"
    then
        lzip -9 "${destdir}/${mandir}"/man?/*
    fi

    # Delete temporary build directories
    cd -- "$TMPDIR"
    rm -rf ./mksh-build ./lksh-build
}

